package ae.pcfc.cldos.online.common.manager.provider;

import org.springframework.ws.client.WebServiceIOException;
import org.springframework.ws.soap.client.SoapFaultClientException;

import ae.pcfc.cldos.online.common.clddb.domain.manager.AppMetaDataDOM;
import ae.pcfc.cldos.online.common.exception.EmployeeAllreadyExistException;
import ae.pcfc.cldos.online.common.exception.EmployeeIsBanedException;
import ae.pcfc.cldos.online.common.exception.EmployeeQuotaException;
import ae.pcfc.cldos.online.common.exception.GSAppException;
import ae.pcfc.cldos.online.common.exception.InsufficientBGException;
import ae.pcfc.cldos.online.common.exception.InvalidClientException;
import ae.pcfc.cldos.online.common.exception.LeaseExpireException;
import ae.pcfc.cldos.online.common.exception.LicenseErrorRemarkException;
import ae.pcfc.cldos.online.common.exception.LicenseExpiredException;
import ae.pcfc.cldos.online.common.exception.QoutaAllowedException;
import ae.pcfc.cldos.online.common.exception.QoutaReqAllowedException;
import ae.pcfc.cldos.online.common.exception.RPExpiredException;
import ae.pcfc.cldos.online.common.exception.RequestAllreadyExistException;
import ae.pcfc.cldos.online.common.exception.UpdateClientDtlException;
import ae.pcfc.cldos.online.common.exception.VisitorAllreadyExistException;
import ae.pcfc.cldos.online.common.manager.BaseManager;
import ae.pcfc.cldos.online.common.manager.BuisnessValidationManager;
import ae.pcfc.cldos.online.gs.ConstantsGS;
import ae.pcfc.cldos.online.net.ConstantsNET;
import ae.pcfc.cldos.online.net.service.BuisnessValidationsService;

/**
 * 
 * @author arosa
 *
 */
public class BuisnessValidationManagerImpl extends BaseManager implements BuisnessValidationManager {

	private AppMetaDataDOM appMetaDataDOM;
	
	public void setAppMetaDataDOM(AppMetaDataDOM appMetaDataDOM) {
		this.appMetaDataDOM = appMetaDataDOM;
	}
	
	private BuisnessValidationsService buisnessValidationsService;
	
	public void setBuisnessValidationsService(
			BuisnessValidationsService buisnessValidationsService) {
		this.buisnessValidationsService = buisnessValidationsService;
	}
	/**
	 * @param input
	 * @return
	 */
	private String formatInputValidation(String input){
		String outputString = input;
		if(input.contains("'")){
			outputString = input.replace("'", "''");
		}
		return outputString;
	}
	/**
	 * @param companyLicenseNo
	 * @return
	 * @throws Exception
	 */
	public void validateOnlineNOCLettersData(String companyLicenseNo) throws Exception{
		try{
			boolean isValid = buisnessValidationsService.validateOnlineNOCLettersData(companyLicenseNo);
			if(!isValid){
				throw new Exception();
			}
		}catch(Exception e){
			e.printStackTrace();
			String errorCode = e.getMessage();
			if(errorCode.contains(ConstantsGS.ERROR_BR_NOC_LIC_ERR1) || errorCode.contains(ConstantsGS.ERROR_BR_NOC_LIC_ERR3) || errorCode.contains(ConstantsGS.ERROR_BR_NOC_LIC_ERR4) ){
				throw new LicenseExpiredException();
			}else if(errorCode.contains(ConstantsGS.ERROR_BR_NOC_LIC_ERR2)){
				throw new LeaseExpireException();
			}else if(errorCode.contains(ConstantsGS.ERROR_BR_NOC_LIC_ERR_REMARK)){
				throw new LicenseErrorRemarkException();
			}else{
				throw new Exception(e);
			}
		}
	}
	
	/**
	 * @param companyLicenseNo
	 * @param empId
	 * @throws Exception
	 */
	public void validateOnlineEmpVisaTerminationData(String companyLicenseNo) throws Exception{
		try{
			boolean isValid = buisnessValidationsService.validateOnlineEmpVisaTerminationData(companyLicenseNo);
			if(!isValid){
				throw new Exception();
			}
		}catch(Exception e){
			e.printStackTrace();
			String errorCode = e.getMessage();
			if(errorCode.contains(ConstantsGS.ERROR_BR_EVR_LIC_ERR1) || errorCode.contains(ConstantsGS.ERROR_BR_EVR_LIC_ERR2)){
				throw new LicenseExpiredException();
			}else if(errorCode.contains(ConstantsGS.ERROR_BR_EVR_LIC_ERR3)){
				throw new LicenseErrorRemarkException();
			}else{
				throw new Exception(e);
			}
		}
	}
	
	/**
	 * @param companyLicenseNo
	 * @param empId
	 * @throws Exception
	 */
	public void validateOnlineEmpVisaRenewalData(String companyLicenseNo, String empId) throws Exception{
		try{
			boolean isValid = buisnessValidationsService.validateOnlineEmpVisaRenewalData(companyLicenseNo, empId);
			if(!isValid){
				throw new Exception();
			}
		}catch(Exception e){
			e.printStackTrace();
			String errorCode = e.getMessage();
			if(errorCode.contains(ConstantsGS.ERROR_BR_EVR_LIC_ERR1) || errorCode.contains(ConstantsGS.ERROR_BR_EVR_LIC_ERR2)){
				throw new LicenseExpiredException();
			}else if(errorCode.contains(ConstantsGS.ERROR_BR_EVR_LIC_ERR3)){
				throw new LicenseErrorRemarkException();
			}else if(errorCode.contains(ConstantsGS.ERROR_BR_EVR_GEN_ERR)){
				throw new InvalidClientException();
			}else if (errorCode.contains(ConstantsGS.ERROR_BR_EVR_RP_ERR)){
				throw new RPExpiredException();
			}else{
				throw new Exception(e);
			}
		}
	}
	
	/**
	 * @param companyLicenseNo
	 * @param passportNameEn
	 * @param passportNo
	 * @return
	 * @throws Exception
	 */
	public void validateOnlineVisitVisaData(String companyLicenseNo) throws Exception{
		try{
			boolean isValid = buisnessValidationsService.validateOnlineVisitVisaData(companyLicenseNo);
			if(!isValid){
				throw new Exception();
			}
		}catch(Exception e){
			e.printStackTrace();
			String errorCode = e.getMessage();
			if(errorCode.contains(ConstantsGS.ERROR_BR_LIC_ERR1) || errorCode.contains(ConstantsGS.ERROR_BR_LIC_ERR2) || 
					errorCode.contains(ConstantsGS.ERROR_BR_LIC_ERR3) || errorCode.contains(ConstantsGS.ERROR_BR_LIC_ERR4)){
				throw new LicenseExpiredException();
			}else if(errorCode.contains(ConstantsGS.ERROR_BR_LIC_ERR_REMARK)){
				throw new LicenseErrorRemarkException();
			}else if(errorCode.contains(ConstantsGS.ERROR_BR_UPDATE_CLIENT)){
				throw new UpdateClientDtlException();
			}else if (errorCode.contains(ConstantsGS.ERROR_BR_INSUFF_BG)){
				e.printStackTrace();
			}else{
				throw new Exception(e);
			}
		}
	}
	/**
	 * @param companyLicenseNo
	 * @param passportNameEn
	 * @param passportNo
	 * @return
	 * @throws Exception
	 */
	public void validateOnlineEmpVisaData(String companyLicenseNo, String passportNameEn, String passportNo) throws Exception{
		try{
			boolean isValid = buisnessValidationsService.validateOnlineEmpVisaData(companyLicenseNo, formatInputValidation(passportNameEn), formatInputValidation(passportNo));
			if(!isValid){
				throw new Exception();
			}
		}catch(Exception e){
			e.printStackTrace();
			String errorCode = e.getMessage();
			if(errorCode.contains(ConstantsGS.ERROR_BR_12_INVALID_CLIENT)){
				throw new InvalidClientException();
			}else if(errorCode.contains(ConstantsGS.ERROR_BR_11_LIC_INAV)){
				throw new LicenseExpiredException();
			}else if(errorCode.contains(ConstantsGS.ERROR_BR_12_ERR_REMARK)){
				throw new LicenseErrorRemarkException();
			}else if(errorCode.contains(ConstantsGS.ERROR_BR_35_ACT_GRTR_REQALLWD)){
				throw new QoutaReqAllowedException();
			}else if(errorCode.contains(ConstantsGS.ERROR_BR_35_ACT_GRTR_ALLWD)){
				throw new QoutaAllowedException();
			}else if(errorCode.contains(ConstantsGS.ERROR_BR_13_INSUFFCNT_BG)){
				throw new InsufficientBGException();
			}else if(errorCode.contains(ConstantsGS.ERROR_BR_13_1)){
				throw new UpdateClientDtlException();
			}else if(errorCode.contains(ConstantsGS.ERROR_BR_EMP_BANNED)){
				throw new EmployeeIsBanedException();
			}else{
				throw new Exception(e);
			}
		}
	}
	public void validateEmployee(String passportName, String passportNo,boolean isVisitVisa) 
			throws RequestAllreadyExistException, EmployeeAllreadyExistException, GSAppException, VisitorAllreadyExistException{
	/*	if(appMetaDataDOM.isEmployeeHasRequest(passportName, passportNo)){
			throw new RequestAllreadyExistException();
		}*/
		try{
			if(!isVisitVisa){
				if(buisnessValidationsService.isEmployeeExist(formatInputValidation(passportName),formatInputValidation(passportNo))){
					throw new EmployeeAllreadyExistException();
				}
			} else {
				if(buisnessValidationsService.isVisitorExist(formatInputValidation(passportName),formatInputValidation(passportNo))){
					throw new VisitorAllreadyExistException();
				}
			}
		}catch (WebServiceIOException e) {
			throw new GSAppException(e);
		}catch (SoapFaultClientException e) {
			throw new GSAppException(e);
		}
	}
	
	public void validateEmployee(String gsEmployeeId) throws RequestAllreadyExistException{
		if(appMetaDataDOM.isEmployeeHasRequest(gsEmployeeId)){
			throw new RequestAllreadyExistException();
		}
	}
	
	public void validateCompany(String coLicNo) throws LeaseExpireException, LicenseExpiredException,LicenseErrorRemarkException{
		if(!buisnessValidationsService.isLeaseValid(coLicNo)){
			throw new LeaseExpireException();
		}
		String licStatus = buisnessValidationsService.isLicenceValid(coLicNo);
		if(licStatus.equalsIgnoreCase(ConstantsNET.LIC_ERROR_REMARK)){
			throw new LicenseErrorRemarkException();
		}else if(licStatus.equalsIgnoreCase(ConstantsNET.LIC_NOT_VALID)){
			throw new LicenseExpiredException();
		}
	}
	
	public void validateEmployeeQuota(String coLicNo, Long clientId) throws EmployeeQuotaException{
		Integer aviableEmployees = buisnessValidationsService.getAviableEmployeesCount(coLicNo);
		if(aviableEmployees<=0){
			throw new EmployeeQuotaException();
		}
	}
	/**
	 * @param coLicNo
	 * @return
	 */
	public String getNOCLetterCode(String nocType, String textFormat){
		String nocCOde = null;
		if(nocType.equals(ConstantsNET.NOC_TYPE_BA)){
			nocCOde = ConstantsNET.NOC_TYPE_BA_CODE;
		}else if(nocType.equals(ConstantsNET.NOC_TYPE_LLR)){
			nocCOde = ConstantsNET.NOC_TYPE_LLR_CODE;
		}else if(nocType.equals(ConstantsNET.NOC_TYPE_LR)){
			nocCOde = ConstantsNET.NOC_TYPE_LR_CODE;
		}else if(nocType.equals(ConstantsNET.NOC_TYPE_NDL)){
			nocCOde = ConstantsNET.NOC_TYPE_NDL_CODE;
		}else if(nocType.equals(ConstantsNET.NOC_TYPE_RCR)){
			nocCOde = ConstantsNET.NOC_TYPE_RCR_CODE;
		}else if(nocType.equals(ConstantsNET.NOC_TYPE_SCR)){
			nocCOde = ConstantsNET.NOC_TYPE_SCR_CODE;
		}else if(nocType.equals(ConstantsNET.NOC_TYPE_TCR)){
			nocCOde = ConstantsNET.NOC_TYPE_TCR_CODE;
		}else if(nocType.equals(ConstantsNET.NOC_TYPE_TLR)){
			nocCOde = ConstantsNET.NOC_TYPE_TLR_CODE;
		}else if(nocType.equals(ConstantsNET.NOC_TYPE_WCR)){
			nocCOde = ConstantsNET.NOC_TYPE_WCR_CODE;
		}else if(nocType.equals(ConstantsNET.NOC_TYPE_CR) && textFormat.equals("Arabic")){
			nocCOde = ConstantsNET.NOC_TYPE_CR_CODE_AR;
		}else{
			nocCOde = ConstantsNET.NOC_TYPE_CR_CODE_EN;
		}
		return nocCOde;
	}
}
